13. Heap code in C++ template <class eType> class Heap { public: Heap( int capacity = 100 ); void insert( const eType & x ); void deleteMin( eType & minItem ); const eType & getMin( ); bool isEmpty( ); bool isFull( ); int Heap<eType>::getSize( ); http://ecomputernotes.com
14. Heap code in C++ private: int currentSize; // Number of elements in heap eType* array; // The heap array int capacity; void percolateDown( int hole ); }; http://ecomputernotes.com
15. Heap code in C++ #include "Heap.h“ template <class eType> Heap<eType>::Heap( int capacity ) { array = new etype[capacity + 1]; currentSize=0; } http://ecomputernotes.com
16. Heap code in C++ // Insert item x into the heap, maintaining heap // order. Duplicates are allowed. template <class eType> bool Heap<eType>::insert( const eType & x ) { if( isFull( ) ) { cout << "insert - Heap is full." << endl; return 0; } // Percolate up int hole = ++currentSize; for(; hole > 1 && x < array[hole/2 ]; hole /= 2) array[ hole ] = array[ hole / 2 ]; array[hole] = x; } http://ecomputernotes.com